<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		<script type="text/javascript">
					
			var arr = [1,2,3,4,5,6,7,8,9,10,11,23,44,86]
			// 非递归算法
			function get(arr, key) {
				for (var low = 0, high = arr.length-1; low <= high;) {
					var mid = parseInt((low + high) / 2)
					if (key === arr[mid]) {
						return mid
					} else if (key < arr[mid]) {
						high = mid - 1
					} else if (key > arr[mid]) {
						low = mid + 1
					} else {
						return -1
					}
				}
			}
			// 递归算法
			function get (arr, key, low = 0, high = arr.length-1) {
				if (low > high) return -1
				var mid = parseInt((low + high) / 2)
				if (key === arr[mid]) {
					return mid
				} else if (key < arr[mid]) {
					high = mid - 1
					return get(arr,key,low,high)
				} else if (key > arr[mid]) {
					low = mid + 1
					return get(arr,key,low,high)
				} else {
					return -1
				}
			}
			
			
			
			
			
			// var arr = [1,2,3,4,5,6,7,8,9,10,11,23,44,86]
			
			function get(arr, key) {
				var low = 0
				var high = arr.length-1
				
				while (low <= high) {
					var mid = parseInt((low + high) / 2)
					if (key === arr[mid]) {
						return mid
					} else if (key < arr[mid]) {
						high = mid - 1
					} else if (key > arr[mid]) {
						low = mid + 1
					} else {
						return -1
					}
				}
			}
			
			console.time('a')
			var reslut1 = get(arr, 10)
			console.timeEnd('a')
			alert(reslut1)
			console.log(reslut1)
			
			
			
			
			
			
			
			
			
		</script>
	</body>
</html>
